/**
 *
 */
package name.yzhu.wbdata.weibo;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import weibo4j.Timeline;
import weibo4j.model.Paging;
import weibo4j.model.Status;
import weibo4j.model.StatusWapper;

import name.yzhu.common.conf.PropertyConfig;
import name.yzhu.common.yaml.YamlItem;
import name.yzhu.wbdata.Constant;
import name.yzhu.wbdata.io.WeiboYamlRow;

/**
 * @author yhzhu
 *
 */
public class SinaWeiboSDK extends AbstractWeiboSDK {
	private static final Log LOG = LogFactory.getLog(SinaWeiboSDK.class);

	private static final String APP_KEY = "sina.app.key";
	private static final String APP_SECRET = "sina.app.secret";
	private static final String ACCESS_TOKEN = "sina.access.token";

	private static final String PULIC_FREQ = "sina.public.freq";
	private static final String FRIEND_FREQ = "sina.friend.freq";

	private String currentMaxStatusID = "1";

	public SinaWeiboSDK(BlockingQueue<YamlItem> queue) {
		super(queue);
		this.weiboType = WeiboType.SINA;
	}
	/* (non-Javadoc)
	 * @see name.yzhu.wbdata.weibo.AbstractWeiboSDK#open(name.yzhu.common.conf.PropertyConfig)
	 */
	@Override
	public void open(PropertyConfig proCFG) {
		appKey = proCFG.getString(APP_KEY);
		appSecret = proCFG.getString(APP_SECRET);
		accessToken = proCFG.getString(ACCESS_TOKEN);

		setPublicFreq(Integer.valueOf(proCFG.getString(PULIC_FREQ, String.valueOf(getPublicFreq()))));
		setFriendFeq(Integer.valueOf(proCFG.getString(FRIEND_FREQ, String.valueOf(getPublicFreq()))));
	}

	@Override
	public int getPublicFreq() {
		return 30;
	}

	@Override
	public int getFriendFreq() {
		return 180;
	}

	@Override
	public String getDataSource() {
		return WeiboType.SINA.getType();
	}

	@Override
	public List<YamlItem> publicTimeLine() throws WeiboException {
		Timeline tl = new Timeline();
		tl.setToken(getAccessToken());

		try {
			StatusWapper sw = tl.getPublicTimeline(Constant.SINA_MAX_PUBLIC_ITEM_COUNT, 0);
			List<YamlItem> yamls = new ArrayList<YamlItem>(sw.getStatuses().size());
			for (Status s : sw.getStatuses()) {
				YamlItem yaml = statusToYaml(s);
				yamls.add(yaml);
			}
			return yamls;
		} catch (weibo4j.model.WeiboException e) {
			LOG.error(e.getMessage(), e);
			throw new WeiboException(e);
		}
	}

	@Override
	public List<YamlItem> friendTimeLine() throws WeiboException{
		Timeline tl = new Timeline();
		tl.setToken(getAccessToken());

		try {
			Paging p = new Paging(Long.valueOf(this.currentMaxStatusID));
			StatusWapper sw = tl.getFriendsTimeline(0, 0, p);
			List<YamlItem> yamls = new ArrayList<YamlItem>(sw.getStatuses().size());
			for (Status s : sw.getStatuses()) {
				YamlItem yaml = statusToYaml(s);
				yamls.add(yaml);
				if (s.getId().compareTo(currentMaxStatusID) > 0)
	        		currentMaxStatusID = s.getId();
			}
			return yamls;
		} catch (weibo4j.model.WeiboException e) {
			LOG.error(e.getMessage(), e);
			throw new WeiboException(e);
		}
	}

	protected YamlItem statusToYaml(final Status s) {
		YamlItem yaml = new WeiboYamlRow();
		yaml.put(name.yzhu.wbdata.Constant.YAML_FIELD_WEIBO_ID, s.getId());
		if (s.getCreatedAt() != null)
		   yaml.put(name.yzhu.common.Constant.YAML_FIELD_DATE, name.yzhu.common.Constant.
				   TIMESTAMP_FORMAT.format(s.getCreatedAt()));
		yaml.put(name.yzhu.common.Constant.YAML_FIELD_CONTENT, s.getText());
		yaml.put(name.yzhu.wbdata.Constant.YAML_FIELD_WEIBO_FORWARD_COUNT, String.valueOf(s.getRepostsCount()));
		yaml.put(name.yzhu.wbdata.Constant.YAML_FIELD_WEIBO_CONMENT_COUNT, String.valueOf(s.getCommentsCount()));

		if (s.getUser() != null){
			yaml.put(name.yzhu.wbdata.Constant.YAML_FIELD_WEIBO_USER_ID, s.getUser().getId());
			yaml.put(name.yzhu.wbdata.Constant.YAML_FIELD_WEIBO_USER_NICK_NAME, s.getUser().getScreenName());
		}

		if (s.getSource() != null) {
		    yaml.put(name.yzhu.wbdata.Constant.YAML_FIELD_WEIBO_SOURCE_NAME, s.getSource().getName());
//		    yaml.put("source_relationship", s.getSource().getRelationship());
		    yaml.put(name.yzhu.wbdata.Constant.YAML_FIELD_WEIBO_SOURCE_URL, s.getSource().getUrl());
		}

		if (s.getRetweetedStatus() != null) {
			yaml.put(name.yzhu.wbdata.Constant.YAML_FIELD_WEIBO_ORIGINAL_ID, s.getRetweetedStatus().getId());
			yaml.put(name.yzhu.wbdata.Constant.YAML_FIELD_WEIBO_ORIGINAL_TEXT, s.getRetweetedStatus().getText());
			yaml.put(name.yzhu.wbdata.Constant.YAML_FIELD_WEIBO_ORIGINAL_DATE, name.yzhu.common.Constant.
					TIMESTAMP_FORMAT.format(s.getRetweetedStatus().getCreatedAt()));
			if (s.getRetweetedStatus().getUser() != null){
				yaml.put(name.yzhu.wbdata.Constant.YAML_FIELD_WEIBO_ORIGINAL_USER_ID, s.getRetweetedStatus().getUser().getId());
				yaml.put(name.yzhu.wbdata.Constant.YAML_FIELD_WEIBO_ORIGINAL_USER_NICK_NAME, s.getRetweetedStatus().getUser().getScreenName());
			}
		}
		return yaml;
	}

}
